package com.htx.zqs.blesmartmask.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.htx.zqs.blesmartmask.MyApplication;
import com.htx.zqs.blesmartmask.R;
import com.htx.zqs.blesmartmask.bean.Contants;
import com.htx.zqs.blesmartmask.listeners.OkListener;
import com.htx.zqs.blesmartmask.utils.BlEUtile;
import com.htx.zqs.blesmartmask.utils.OkhttpUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by zhao on 2018/7/10.
 */

public class FeedbackActivity extends BaseActivity {
    @InjectView(R.id.ib_back)
    ImageButton  ibBack;
    @InjectView(R.id.tv_title)
    TextView     tvTitle;
    @InjectView(R.id.ib_menu)
    TextView     ibMenu;
    @InjectView(R.id.ll_title)
    LinearLayout llTitle;
    @InjectView(R.id.useropinion_tv)
    TextView     useropinionTv;
    @InjectView(R.id.useropinion_et)
    EditText     useropinionEt;
    @InjectView(R.id.useropinion_ll)
    LinearLayout useropinionLl;
    @InjectView(R.id.useropinion_remaining)
    TextView     useropinionRemaining;
    @InjectView(R.id.useropinion_submit)
    Button       useropinionSubmit;
    private int     MAX    = 500;
    private boolean isback = false;
    private OkhttpUtils feedBackOK;
    private HashMap map = new HashMap();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //解决软键盘弹出界面变型问题
       // getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_feedback);
        ButterKnife.inject(this);
        initView();
    }

    private void initView() {
        feedBackOK = new OkhttpUtils(this, Contants.FEED_BACK, new OkListener() {
            @Override
            public void onSuccess(int requestCode, String string) {
                isback = true;
                useropinionTv.setVisibility(View.VISIBLE);
                useropinionRemaining.setVisibility(View.GONE);
                useropinionLl.setVisibility(View.GONE);
                useropinionSubmit.setBackgroundResource(R.color.blue);
                useropinionSubmit.setText(getResources().getString(R.string.submitted_successfully));
                Log.e("feed=====", "===67");
                ArrayList<View> views = new ArrayList();
                views.add(useropinionEt);
                hideSoftKeyboard(FeedbackActivity.this, views);
            }

            @Override
            public void onError(int requestCode, IOException e) {
                Log.e("feed=====", "===72");
                Toast.makeText(FeedbackActivity.this, getResources().getString(R.string.error_message), Toast.LENGTH_SHORT).show();
            }
        });
        ibBack.setVisibility(View.VISIBLE);
        tvTitle.setText(getResources().getString(R.string.feedback));
        useropinionEt.addTextChangedListener(new TextWatcher() {
            private int selectionStart, selectionEnd;
            private CharSequence temp;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                temp = s;
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String message =getResources().getString(R.string.textchange_input_one) + (MAX - s.toString().length()) + getResources().getString(R.string.textchange_input_two);
                // TODO: 2018/9/8 多语言处理
                 if (MyApplication.lauguage.equals("ko-KR")){
                   message = (MAX - s.toString().length())+"자 입력할수 있습니다.";
                 }
                  useropinionRemaining.setText(message);
            }

            @Override
            public void afterTextChanged(Editable s) {
                selectionStart = useropinionEt.getSelectionStart();
                selectionEnd = useropinionEt.getSelectionEnd();
                if (temp.length() > MAX) {
                    Toast.makeText(FeedbackActivity.this, getResources().getString(R.string.textchange_limit), Toast.LENGTH_SHORT).show();
                    s.delete(selectionStart - 1, selectionEnd);
                    int tempSelection = selectionEnd;
                    useropinionEt.setText(s);
                    useropinionEt.setSelection(tempSelection);
                }
            }
        });
    }

    /**
     * 隐藏软键盘(可用于Activity，Fragment)
     */
    public static void hideSoftKeyboard(Context context, List<View> viewList) {
        if (viewList == null) return;

        InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);

        for (View v : viewList) {
            inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    @OnClick({R.id.ib_back, R.id.useropinion_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ib_back:
//                if (isback) {
//                    InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//                    im.hideSoftInputFromWindow(getCurrentFocus().getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
//                } else {
//                    new Handler().postDelayed(new Runnable() {
//                        @Override
//                        public void run() {
//                            finish();
//                            overridePendingTransition(0, 0);
//                        }
//                    }, 50);
//                }
                finish();
                break;
            case R.id.useropinion_submit:

                String feedBackInfo = useropinionEt.getText().toString().trim();
                if (feedBackInfo != null && feedBackInfo.length() > 0 && feedBackInfo != "") {
                    if (BlEUtile.isNetworkConnected(this)) {
                        isback(feedBackInfo);
                    } else {
                        if (!BlEUtile.isFastClick()) {
                            Toast.makeText(this, getResources().getString(R.string.unnet_message), Toast.LENGTH_SHORT).show();
                        }
                    }
                } else {
                    if (!BlEUtile.isFastClick()) {
                        Toast.makeText(this, getResources().getString(R.string.feedback_message), Toast.LENGTH_SHORT).show();
                    }
                }
                break;
        }
    }

    /*判断按钮功能*/
    private void isback(String feedBackInfo) {
        if (isback) {
            finish();
        } else {
            Log.e("feed=====", "===101");
            map.clear();
            map.put("feedback", feedBackInfo);
            feedBackOK.postForm(Contants.FEED_BACK, Contants.BASE_URL + "feedback.link", map);

        }
    }
}
